﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace file_client
{
    public partial class frmFindingList : Form
    {
        public List<int> ListFiles;
        public frmFindingList()
        {
            InitializeComponent();
        }

        private void frmFindingList_Shown(object sender, EventArgs e)
        {
            try
            {
                this.Text = "Найдено: " + ListFiles.Count.ToString();
                lsvFindingList.Items.Clear();
                int i = 1;
                foreach (int item_id in ListFiles)
                {
                    DBActive item_ref = new DBActive(Properties.Settings.Default.ConnectionString, Properties.Settings.Default.StartFolder, item_id);
                    if (!item_ref.isLoaded) continue;
                    ListViewItem item = lsvFindingList.Items.Add(i.ToString());
                    item.Tag = item_ref;
                    item.SubItems.Add(item_id.ToString());
                    item.SubItems.Add(item_ref.Surname);
                    item.SubItems.Add(item_ref.Name);
                    item.SubItems.Add(item_ref.Nick);
                    item.SubItems.Add(item_ref.Address);
                    i++;
                }
                this.Text = "Найдено:" + (i - 1).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lsvFindingList_DoubleClick(object sender, EventArgs e)
        {
            if (lsvFindingList.SelectedItems.Count != 1) return;
            frmFile form = new frmFile();
            form.id = int.Parse(lsvFindingList.SelectedItems[0].SubItems[1].Text);
            form.Show();
        }

        private void выделитьВсеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lsvFindingList.Items)
            {
                item.Selected = true;
            }
        }

        private void убратьВыделениеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lsvFindingList.Items)
            {
                item.Selected = false;
            }

        }

        private void cmsPrint_Opening(object sender, CancelEventArgs e)
        {
            string folder = Application.StartupPath;
            if (Properties.Settings.Default.ShablonFolderList != "") folder = Properties.Settings.Default.ShablonFolderList;
            печатьToolStripMenuItem.DropDownItems.Clear();
            DirectoryInfo di = new DirectoryInfo(folder);
            foreach (FileInfo item in di.GetFiles("*.dot"))
            {
                if ((item.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
                ToolStripItem menuPrint = печатьToolStripMenuItem.DropDownItems.Add(item.Name.Substring(0, item.Name.Length - item.Extension.Length));
                menuPrint.Click += menuPrint_Click;
            }
        }

        void menuPrint_Click(object sender, EventArgs e)
        {
            try
            {
                string folder = Application.StartupPath;
                if (Properties.Settings.Default.ShablonFolderList != "") folder = Properties.Settings.Default.ShablonFolderList;
                Type TWordApplication = Type.GetTypeFromProgID("Word.Application");
                dynamic wordApplication = Activator.CreateInstance(TWordApplication);
                wordApplication = wordApplication.Application;
                wordApplication.Documents.Add(Path.Combine(folder, (sender as ToolStripItem).Text + ".dot"));
                try
                {
                    wordApplication.Run("Init");
                }
                catch {}
                foreach (ListViewItem item in lsvFindingList.SelectedItems)
                {
                    int id = int.Parse(item.SubItems[1].Text);
                    wordApplication.Run("Start", id);
                }
                try
                {
                    wordApplication.Run("Done");
                }
                catch { }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
